package com.winning.sx.microframework.service.sysmanage;

import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.dao.sysmanage.ConstDao;
import com.winning.sx.microframework.dao.sysmanage.LoginDao;
import com.winning.sx.microframework.model.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ConstService {
    @Autowired
    private ConstDao dao;
    public List queryCount(String type) throws CustomException {
        Map param=new HashMap();
        param.put("type",type);
        return dao.queryCount(param).getDataRows();
    }

    public List query(String type) throws CustomException {
        Map param=new HashMap();
        param.put("code",type);
        return dao.query(param).getDataRows();
    }
    public List queryCode(String code) throws CustomException {
        Map param=new HashMap();
        param.put("code",code);
        return dao.queryCode(param).getDataRows();
    }
    public DataTable queryModel(Map<String, String> map) throws CustomException {
        return dao.queryModel(map);
    }
    public String queryValue(String key) throws CustomException {
        return dao.queryValue(key);
    }
    public Map queryMyOrg(Map param) throws CustomException {
        List list=dao.queryMyOrg(param);
        String orgs=""+getIds(list);
        String allorgs=""+getIds(list);
        if(orgs.length()>0) orgs=orgs.substring(1);
        if(allorgs.length()>0) allorgs=param.get("superOrganizationId").toString()+allorgs;
        else allorgs=param.get("superOrganizationId").toString();
        Map re=new HashMap();
        re.put("list",list);
        re.put("orgs",orgs);
        re.put("allorgs",allorgs);
        return re;
    }
    private String getIds(List list){
        String orgs="";
        for(int i =0;i< list.size();i++){
            orgs+=(",'"+((Map)list.get(i)).get("id").toString()+"'");
            if(((List)((Map)list.get(i)).get("childrenOrg")).size()>0){
                orgs+= getIds((List)((Map)list.get(i)).get("childrenOrg"));
            }
        }
        return orgs;
    }
}
